﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace Chenxin.Lib.Passport.DAL
{
    /// <summary>
    /// 数据访问类System_TraceInfo。
    /// </summary>
    public class System_TraceInfo
    {
        public System_TraceInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Entity.System_TraceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_TraceInfo(");
            strSql.Append("RecordId,OprUserId,OprUserName,URL,IPAddr,Title,Memo,Addtime, QueryString)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@OprUserId,@OprUserName,@URL,@IPAddr,@Title,@Memo,@Addtime,@QueryString)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "OprUserId", DbType.Guid, model.OprUserId);
            db.AddInParameter(dbCommand, "OprUserName", DbType.String, model.OprUserName);
            db.AddInParameter(dbCommand, "URL", DbType.String, model.URL);
            db.AddInParameter(dbCommand, "IPAddr", DbType.AnsiString, model.IPAddr);
            db.AddInParameter(dbCommand, "Title", DbType.AnsiString, model.Title);
            db.AddInParameter(dbCommand, "Memo", DbType.AnsiString, model.Memo);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "QueryString", DbType.AnsiString, model.QueryString);
            
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Entity.System_TraceInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,OprUserId,OprUserName,URL,IPAddr,Title,Memo,Addtime,QueryString from System_TraceInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Entity.System_TraceInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,OprUserId,OprUserName,URL,IPAddr,Title,Memo,Addtime,QueryString ");
            strSql.Append(" FROM System_TraceInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "System_TraceInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Entity.System_TraceInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,OprUserId,OprUserName,URL,IPAddr,Title,Memo,Addtime,QueryString ");
            strSql.Append(" FROM System_TraceInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            strSql.Append(" Order By Addtime DESC");
            List<Entity.System_TraceInfo> list = new List<Entity.System_TraceInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Entity.System_TraceInfo ReaderBind(IDataReader dataReader)
        {
            Entity.System_TraceInfo model = new Entity.System_TraceInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["OprUserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprUserId = new Guid(ojb.ToString());
            }
            model.OprUserName = dataReader["OprUserName"].ToString();
            model.URL = dataReader["URL"].ToString();
            model.IPAddr = dataReader["IPAddr"].ToString();
            model.Title = dataReader["Title"].ToString();
            model.Memo = dataReader["QueryString"].ToString();
            model.Memo = dataReader["Memo"].ToString();
            
            ojb = dataReader["Addtime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Addtime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}
